package com.cnzz.site1255174697.fragment.main;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cnzz.site1255174697.Const;
import com.cnzz.site1255174697.ConstHost;
import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.activity.taobao.SearchActivity;
import com.cnzz.site1255174697.factory.IndexFactory;
import com.cnzz.site1255174697.fragment.MallIndexFragment;
import com.cnzz.site1255174697.fragment.RecomFragment;
import com.cnzz.site1255174697.holder.KindsGridAdapter;
import com.cnzz.site1255174697.model.BaseEntity;
import com.cnzz.site1255174697.model.LogoEntity;
import com.cnzz.site1255174697.utils.SystemBarUtils;
import com.cqyanyu.yanyu.adapter.TabFragmentPagerAdapter;
import com.cqyanyu.yanyu.http.XCallback;
import com.cqyanyu.yanyu.http.XResultList;
import com.cqyanyu.yanyu.model.EventEntity;
import com.cqyanyu.yanyu.utils.XToastUtil;
import com.cqyanyu.yanyu.view.FontIconView;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 */
public class IndexFragment extends Fragment implements View.OnClickListener {

    protected View rootView;
    protected TabLayout mTabLayout;
    protected ViewPager mViewPage;
    protected ImageView btnLeft;//左边Logo
    protected RelativeLayout btSearch;//搜索框
    protected TextView tvTuijian;
    protected FontIconView fvXia;
    protected LinearLayout lineEmbTop;
    protected TextView tvXuanze;
    protected LinearLayout linTuijian;
    private boolean isXuan = true;
    private List<BaseEntity> list;
    private List<Fragment> fragments = new ArrayList<Fragment>();
    private List<String> titles = new ArrayList<String>();
    private KindsGridAdapter adapter;
    private PopupWindow popupWindow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_index, null);
        initView();
        SystemBarUtils.setSystemBarColor(getActivity(), rootView);
        loadData();
        return rootView;
    }

    //初始化与绑定事件
    private void initView() {
        mTabLayout = (TabLayout) rootView.findViewById(R.id.mTabLayout);
        mViewPage = (ViewPager) rootView.findViewById(R.id.mViewPage);
        btnLeft = (ImageView) rootView.findViewById(R.id.btn_left);
        btSearch = (RelativeLayout) rootView.findViewById(R.id.bt_search);
        tvTuijian = (TextView) rootView.findViewById(R.id.tv_tuijian);
        fvXia = (FontIconView) rootView.findViewById(R.id.fv_xia);
        fvXia.setOnClickListener(this);
        tvXuanze = (TextView) rootView.findViewById(R.id.tv_xuanze);
        btSearch.setOnClickListener(this);
        lineEmbTop = (LinearLayout) rootView.findViewById(R.id.line_emb_top);
        linTuijian = (LinearLayout) rootView.findViewById(R.id.lin_tuijian);
        tvTuijian.setOnClickListener(this);
    }

    private void loadData() {
        IndexFactory.getIndexTop(getActivity(), "", new XCallback<XResultList<BaseEntity>>() {
            @Override
            public void onSuccess(XResultList<BaseEntity> result) {
                list = result.data;
                initTop(result.data);
            }

            public void onFinished() {
            }
        });
        IndexFactory.getLogo(getActivity(), new XCallback<LogoEntity>() {
            @Override
            public void onSuccess(LogoEntity result) {
                Picasso.with(getActivity()).load(ConstHost.IMG_BASE_URL + result.getImg()).into(btnLeft);
            }

            @Override
            public void onFinished() {
            }
        });
    }

    private void initTop(final List<BaseEntity> list) {
        //界面
        fragments.add(new MallIndexFragment(list.get(0).getKey_id()));
        titles.add("精选");
        for (int i = 1; i < list.size(); i++) {
            fragments.add(new RecomFragment(list.get(i).getKey_id(), list.get(i).getName()));
            titles.add(list.get(i).getName());
        }
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mViewPage.setAdapter(new TabFragmentPagerAdapter(getChildFragmentManager(), fragments, titles));
        mTabLayout.setupWithViewPager(mViewPage);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fv_xia:
                if (isXuan) {
                    tvXuanze.setVisibility(View.VISIBLE);
                    mTabLayout.setVisibility(View.GONE);
                    fvXia.setText(getString(R.string.ic_shang));
                    showKindsDialog();
                    linTuijian.setVisibility(View.GONE);
                } else {
                    tvXuanze.setVisibility(View.GONE);
                    mTabLayout.setVisibility(View.VISIBLE);
                    fvXia.setText(getString(R.string.ic_xia));
                    linTuijian.setVisibility(View.VISIBLE);
                }
                isXuan = !isXuan;
                break;
            case R.id.bt_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.tv_tuijian:
                mViewPage.setCurrentItem(0);
                break;
        }
    }


    //分类弹窗
    private void showKindsDialog() {
        View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_kinds, null);
        popupWindow = new PopupWindow(rootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);  //设置点击空白处消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAsDropDown(fvXia);//设置位置
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                tvXuanze.setVisibility(View.GONE);
                mTabLayout.setVisibility(View.VISIBLE);
                fvXia.setText(getString(R.string.ic_xia));
            }
        });
        popupWindow.setFocusable(false);
        GridView kGridView = (GridView) rootView.findViewById(R.id.kGridView);
        FontIconView tvGengguo = (FontIconView) rootView.findViewById(R.id.tv_gengguo);
        adapter = new KindsGridAdapter(getActivity(), list);
        kGridView.setAdapter(adapter);
        tvGengguo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                EventEntity eventEntity = new EventEntity();
                eventEntity.type = Const.JUMP_KINDS;
                EventBus.getDefault().post(eventEntity);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventEntity eventEntity) {
        if (eventEntity != null && eventEntity.type == Const.JUMP_CLICK) {
            adapter.setSelect(eventEntity.id);
            mViewPage.setCurrentItem(eventEntity.id);
            mTabLayout.setupWithViewPager(mViewPage);
            popupWindow.dismiss();
            linTuijian.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
